// pages/home/trace/trace.js
var t = require("../../../utils/formate.js"), e = (getApp().first, getApp()), a = require("../../../utils/config.js"), i = require("../../../utils/amap-wx.js"), n = (getApp().getPromission,
  getApp().getAccessToken);

Page({

  /**
   * 页面的初始数据
   */
  data: {
    lat: 23.099994,
    lng: 113.32452,
    sign: !1,
    status: "",
    data: "",
    height: "92vh",
    markers: [{}, {}],
    polyline: [],
    controls: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    (i = this).arr = [], i.first = 1, i.f = 1, wx.getSystemInfo({
      success: function (t) {
        var a = i.data.controls;
        i.windowHeight = t.windowHeight, a.push({
          id: 1,
          iconPath: "/images/" + e.system.image_prefix + "/position_car.png",
          position: {
            left: t.windowWidth - 65,
            top: 15,
            width: 50,
            height: 50
          },
          clickable: !0
        }, {
            id: 3,
            iconPath: "/images/" + e.system.image_prefix + "/navigation.png",
            position: {
              left: t.windowWidth - 60,
              top: 75,
              width: 40,
              height: 40
            },
            clickable: !0
          }), i.setData({
            controls: a
          });
      }
    });
    var a = t.imei, i = this, n = this.data.polyline;
    this.setData({
      polyline: n
    }), this.setData({
      imei: a
    }, function () {
      new Promise(function (t, e) {
        i.getCarInfo(), t();
      }).then(function () {
        i.setInterval();
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.positionCar);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.positionCar);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  _getCarInfo: function () {
    var a = this, i = this.data.imei;
    wx.request({
      url: e.system.app_url_prefix + "/rest/user2c/device/realtime/location?imei=" + i + "&mapType=" + e.mapType + "&isNeedAddress=true&accessToken=" + wx.getStorageSync("accessToken"),
      method: "GET",
      header: {
        Accept: "application/json",
        "Content-Type": "application/json; charset=utf-8"
      },
      success: function (i) {
        if (i.data) {
          var n = a.data, s = n.markers, o = n.polyline, r = n.status, c = i.data.deviceStatusBean.lat, d = i.data.deviceStatusBean.lng;
          a.ownLat = i.data.deviceStatusBean.lat, a.ownLng = i.data.deviceStatusBean.lng;
          var g = !!i.data.deviceStatusBean.accStatus && i.data.deviceStatusBean.accStatus;
          if (i.data.deviceStatusBean.chargePercentage) var l = i.data.deviceStatusBean.chargePercentage;
          var u = i.data.deviceStatusBean.lat, p = i.data.deviceStatusBean.lng;
          wx.createMapContext("map").getRegion({
            success: function (t) {
              var e = t.southwest.latitude, i = t.southwest.longitude, n = t.northeast.latitude, s = t.northeast.longitude;
              (u <= e || u >= n || p >= s || p <= i) && a.setData({
                lat: u,
                lng: p
              });
            }
          });
          var h = i.data.isWireless, f = t.formatTime(i.data.deviceStatusBean.gpsTime, "Y/M/D h:m:s");
          a.arr.push({
            longitude: d,
            latitude: c
          }), a.arr.length >= 1 && (o[0] = {
            points: a.arr,
            color: "#00ff00",
            width: 4
          });
          var m = "/images/" + e.system.image_prefix + "/new_blue.png";
          switch (i.data.deviceStatusBean.status) {
            case 0:
              m = "/images/" + e.system.image_prefix + "/new_blue.png";
              break;

            case 1:
              m = "/images/" + e.system.image_prefix + "/new_during.png";
              break;

            case 10:
              m = "/images/" + e.system.image_prefix + "/new_off_line.png";
              break;

            case 2:
              m = "/images/" + e.system.image_prefix + "/new_sleep.png";
          }
          r = e.globalData(i.data.deviceStatusBean.status);
          var v = t.getFormattedTime((Date.parse(new Date()) - i.data.deviceStatusBean.gpsTime) / 1e3);
          a.first && wx.showLoading({
            title: "加载中"
          }), (s = a.data.markers)[0].id = 1, s[0].latitude = c, s[0].longitude = d, s[0].iconPath = m,
            s[0].height = e.system.markerWidth ? e.system.markerWidth : 55.43, s[0].width = e.system.markerHeight ? e.system.markerHeight : 30.86,
            s[0].anchor = {
              x: 1,
              y: 1
            }, s[0].rotate = i.data.deviceStatusBean.course, a.setData({
              chargePercentage: l,
              statusTime: v,
              isWireless: h,
              data: i.data,
              status: r,
              time: f,
              ownLng: d,
              ownLat: c,
              markers: s,
              acc: g,
              polyline: o
            }, function () {
              a.route(i.data.deviceStatusBean);
            });
        }
      },
      fail: function (t) { }
    });
  },

  getCarInfo: function () {
    n(this._getCarInfo);
  },

  route: function (t) {
    if (t) {
      var e = this;
      wx.getLocation({
        type: "gcj02",
        success: function (a) {
          var i = a.latitude, n = a.longitude;
          e.amap(i, n, t);
        }
      });
    }
  },

  setInterval: function (t) {
    function e() {
      return t.apply(this, arguments);
    }
    return e.toString = function () {
      return t.toString();
    }, e;
  }(function () {
    var t = this;
    this.positionCar = setInterval(function () {
      t.getCarInfo();
    }, 1e4);
  }),

  controltap: function (t) {
    var a, i, n = this;
    if (1 == t.controlId) i = 2, a = "/images/" + e.system.image_prefix + "/position_my.png",
      wx.getLocation({
        type: "gcj02",
        success: function (t) {
          var a = t.latitude, i = t.longitude, s = n.data.markers;
          s[1].id = 10, s[1].latitude = a, s[1].longitude = i, s[1].iconPath = "/images/" + e.system.image_prefix + "/mine.png",
            s[1].height = 30.86, s[1].width = 30.86, s[1].anchor = {
              x: .5,
              y: .1
            }, n.setData({
              lat: a,
              lng: i,
              markers: s
            });
        }
      }); else if (2 == t.controlId) {
        i = 1, a = "/images/" + e.system.image_prefix + "/position_car.png";
        var s = this.data, o = s.ownLat, r = s.ownLng;
        this.setData({
          lat: o,
          lng: r
        });
      }
    if (2 == t.controlId || 1 == t.controlId) {
      this.data.sign;
      var c = this.data.controls;
      c[0].id = i, c[0].iconPath = a, this.setData({
        controls: c
      });
    }
    if (3 == t.controlId) {
      var d = this.data, o = d.ownLat, r = d.ownLng, g = d.data;
      wx.openLocation({
        latitude: Number(o),
        longitude: Number(r),
        name: g.gpsAddress,
        address: g.gpsAddress,
        scale: 28
      });
    }
  },

  bindupdated: function (t) {
    var e = this;
    wx.createSelectorQuery().select("#header_info").boundingClientRect(function (t) {
      e.data.height;
      e.setData({
        height: e.windowHeight - t.height + "px"
      });
    }).exec();
  },

  amap: function (t, n, s) {
    var o = this, r = a.Config.key;
    new i.AMapWX({
      key: r
    }).getDrivingRoute({
      origin: n + "," + t,
      destination: s.lng + "," + s.lat,
      success: function (t) {
        var a = o.data.polyline, i = [];
        if (t.paths && t.paths[0] && t.paths[0].steps) for (var n = t.paths[0].steps, s = 0; s < n.length; s++) for (var r = n[s].polyline.split(";"), c = 0; c < r.length; c++) i.push({
          longitude: parseFloat(r[c].split(",")[0]),
          latitude: parseFloat(r[c].split(",")[1])
        });
        for (var d = (g = i.length) % 2 != 0; g >= 7e3;) var g = (i = i.filter(function (t, e) {
          return e % 2 == d || 0 === e || e === g - 1;
        })).length;
        a[1] = {
          points: i,
          color: "#FF0000DD",
          width: 2
        }, 1 == o.f && (wx.createMapContext("map").includePoints({
          padding: [100, 100, 100, 100],
          points: i
        }), wx.hideLoading(), o.f = 0), o.first = 0, o.setData({
          polyline: a,
          distance: t.paths[0].distance
        }, function () {
          wx.getLocation({
            type: "gcj02",
            success: function (t) {
              var a = t.latitude, i = t.longitude, n = o.data.markers;
              n[1].id = 10, n[1].latitude = a, n[1].longitude = i, n[1].iconPath = "/images/" + e.system.image_prefix + "/mine.png",
                n[1].height = 30.86, n[1].width = 30.86, n[1].anchor = {
                  x: .5,
                  y: .1
                }, o.setData({
                  markers: n
                });
            }
          });
        });
      },
      fail: function (t) { }
    });
  }

})